<template>
	<view  style="width: 90%; margin: 50rpx auto;">
		<uni-forms :modelValue="formData" ref="form">
			<uni-forms-item name="pass" label="原密码">
				<uni-easyinput v-model="formData.pass" type="password" />
			</uni-forms-item>
			<uni-forms-item name="newPass" label="新密码">
				<uni-easyinput v-model="formData.newPass" type="password" />
			</uni-forms-item>
			<uni-forms-item name="checknNewPass" label="确认新密码">
				<uni-easyinput v-model="formData.checknNewPass" type="password" />
			</uni-forms-item>
		</uni-forms>
		<button type="primary" @click="submit" style="margin: 10rpx;">保存</button>
		<button type="primary" style="margin: 10rpx;" @click="change">取消</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				formData: {
					newPass: '',
					checknNewPass: '',
					pass: '',
				},
				form: {},
				rules: {
					newPass: {
						rules: [{
							required: true,
							errorMessage: '请输入新密码',
						}, {
							validateFunction: (rule, value, data, callback) => {
								return new Promise((resolve, reject) => {
									if (this.formData.checknNewPass !== '') {
										this.$refs.form.validateField('checknNewPass').then((res) => {
											// 成功返回，res 为对应表单数据
											// 其他逻辑处理 
											// ...
										}).catch((err) => {
											// 表单校验验失败，err 为具体错误信息
											// 其他逻辑处理
											// ...
										});
									}
									resolve()
								})
							}
						}]
					},
					checknNewPass: {
						rules: [{
							required: true,
							errorMessage: '请再次输入新密码',
						}, {
							validateFunction: (rule, value, data, callback) => {
								return new Promise((resolve, reject) => {
									if (value == this.formData.newPass) {
										resolve()
									} else {
										reject(new Error('两次输入密码不一致!'))
									}
								})
							}
						}]
					},
					pass: {
						rules: [{
							required: true,
							errorMessage: '请输入原密码',
						}, {
							validateFunction: (rule, value, data, callback) => {
								return new Promise((resolve, reject) => {
									if (value !== this.form.password) {
										reject(new Error('原密码不对!'))
									} else {
										resolve()
									}
								})
							}
						}]
					},
				}
			}
		},
		onLoad() {
			this.form = uni.getStorageSync('user');
			this.loading();
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		methods: {
			/**
			 * 表单提交
			 * @param {Object} event
			 */
			change(){
				uni.switchTab({
					url: '/pages/mine/mine'
				})
			},
			loading() {
				this.request({
					url: `/user/${this.form.id}`
				}).then(res => {
					this.form.password = res.data.password;
				});
			},
			submit() {
				this.$refs.form.validate().then(res => {
					if (this.form.password == this.formData.checknNewPass) {
						uni.showToast({
							title: '新原密码不能相同!'
						})
					} else {
						this.form.password = this.formData.checknNewPass;
						console.log('6666');
						console.log(this.form);
						this.request({
							url: '/user',
							method: 'PUT',
							data: this.form
						}).then(res => {
							if (res.code === '0') {

								uni.navigateTo({
									url: '/pages/mine/myperson'
								})
								uni.showToast({
									title: '重置密码成功'
								})
							}
						})
					}
					console.log('表单数据信息：', res);
				}).catch(err => {
					// uni.hideLoading()
					console.log('表单错误信息：', err);
				})
			}
		}
	}
</script>
